package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.readAndParseXML.service.IDlzyService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwDlzyService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.DlzyForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  道路占用 交换
 * @author Administrator
 *
 */
@Service
@Transactional
public class DlzyServiceImpl implements IDlzyService {
	Logger logger = (Logger) LoggerFactory.getLogger(SsgdServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwDlzyService ywDlzyService;
	
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 

	// xml存放路径
	private final String dlzyfilepath = localResource.getString("dlzyfilepath");

	// 附件存放路径
	private final String dlzyfujianpath = localResource.getString("dlzyfujianpath");

	// 解析成功反馈xml存放路径
	private final String dlzyfeedbackPath = localResource.getString("dlzyfeedbackPath");

	// 解析异常xml存放路径
	private final String dlzyexceptionPath = localResource.getString("dlzyexceptionPath");

	// 解析后，xml备份
	private final String dlzybackPath = localResource.getString("dlzybackPath");

	private Map<String, String> dlzyKeyMap;// xml内容的key

	private Map<String, String> dlzyKeyMapHead;// xml头部的key

	private Map<String, Long> dlzyfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void inithwShMap() {
		// 初始化数据部分对应的map
		dlzyKeyMap = new HashMap<String, String>();
		dlzyKeyMap.put("SQDW", "SQDW");
		dlzyKeyMap.put("YSL_NO", "YSL_NO");
		dlzyKeyMap.put("approvalNo", "approvalNo");
		dlzyKeyMap.put("businessCode", "businessCode");
		dlzyKeyMap.put("sendTime", "sendTime");
		
		dlzyKeyMap.put("SQDW_ADDRESS", "SQDW_ADDRESS");
		dlzyKeyMap.put("SQDW_TEL", "SQDW_TEL");
		dlzyKeyMap.put("FZR", "FZR");
		dlzyKeyMap.put("LXR", "LXR");
		dlzyKeyMap.put("WHMC", "WHMC");
		dlzyKeyMap.put("ZYLMLD", "ZYLMLD");
		dlzyKeyMap.put("RXD_LENGTH", "RXD_LENGTH");
		dlzyKeyMap.put("RXD_WIDTH", "RXD_WIDTH");
		dlzyKeyMap.put("RXD_AREA", "RXD_AREA");
		dlzyKeyMap.put("GGCS_LENGTH", "GGCS_LENGTH");
		dlzyKeyMap.put("GGCS_WIDTH", "GGCS_WIDTH");
		dlzyKeyMap.put("GGCS_AREA", "GGCS_AREA");
		dlzyKeyMap.put("ZYSJQSSJ", "ZYSJQSSJ");
		dlzyKeyMap.put("ZYSJJSSJ", "ZYSJJSSJ");
		dlzyKeyMap.put("JGXSSM", "JGXSSM");
		dlzyKeyMap.put("JGBMYJ", "JGBMYJ");
		dlzyKeyMap.put("YJRQ", "YJRQ");
		dlzyKeyMap.put("TBRQ", "TBRQ");
		dlzyKeyMap.put("ZYDW", "ZYDW");
		dlzyKeyMap.put("ZYDW_ADDRESS", "ZYDW_ADDRESS");
		dlzyKeyMap.put("ZYDW_TEL", "ZYDW_TEL");
		dlzyKeyMap.put("AQFZR", "AQFZR");
		dlzyKeyMap.put("ZYDW_LXR", "ZYDW_LXR");

		

		// 初始化头部对应的map
		dlzyKeyMapHead = new HashMap<String, String>();
		dlzyKeyMapHead.put("sn", "sn");
		dlzyKeyMapHead.put("sender", "sender");
		// dlzyKeyMapHead.put("receiver", "receiver");
		dlzyKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		dlzyfujianMap = new HashMap<String, Long>();
		dlzyfujianMap.put("FJ_01", 315l);
		dlzyfujianMap.put("FJ_02", 316l);
		dlzyfujianMap.put("FJ_03", 317l);
		dlzyfujianMap.put("FJ_04", 318l);
		dlzyfujianMap.put("FJ_05", 319l);
	}

	public void readXml(String projectPath) {
		if (dlzyKeyMap == null)
			this.inithwShMap();
		File file = new File(dlzyfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(dlzyKeyMap, filePath,dlzyfeedbackPath, dlzyexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(dlzyKeyMapHead,files[i], dlzyfeedbackPath, dlzyexceptionPath);

						// 把数据部分保存到数据库
						DlzyForm form = new DlzyForm();
						for (String mapKey : dlzyKeyMap.keySet()) {

							// TODO 把值放到form中
							form.setSqdw(parseXMLService.getMapValue(dlzyKeyMap.get("SQDW")));		
							System.out.println("SQDW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+parseXMLService.getMapValue(dlzyKeyMap.get("SQDW")));
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(dlzyKeyMap.get("SQDW")),"csdllszysp");
							if(ywComQyxxForm != null){
								form.setQybh(ywComQyxxForm.getId().toString());
							}
							form.setYslNo(parseXMLService.getMapValue(dlzyKeyMap.get("YSL_NO")));
							form.setSqdwAddress(parseXMLService.getMapValue(dlzyKeyMap.get("SQDW_ADDRESS")));
							form.setSqdwTel(parseXMLService.getMapValue(dlzyKeyMap.get("SQDW_TEL")));
							form.setFzr(parseXMLService.getMapValue(dlzyKeyMap.get("FZR")));
							form.setLxr(parseXMLService.getMapValue(dlzyKeyMap.get("LXR")));
							form.setWhmc(parseXMLService.getMapValue(dlzyKeyMap.get("WHMC")));
							form.setZylmld(parseXMLService.getMapValue(dlzyKeyMap.get("ZYLMLD")));
							form.setRxdLength(parseXMLService.getMapValue(dlzyKeyMap.get("RXD_LENGTH")));
							form.setRxdWidth(parseXMLService.getMapValue(dlzyKeyMap.get("RXD_WIDTH")));
							form.setRxdArea(parseXMLService.getMapValue(dlzyKeyMap.get("RXD_AREA")));
							form.setGgcsLength(parseXMLService.getMapValue(dlzyKeyMap.get("GGCS_LENGTH")));
							form.setGgcsWidth(parseXMLService.getMapValue(dlzyKeyMap.get("GGCS_WIDTH")));
							form.setGgcsArea(parseXMLService.getMapValue(dlzyKeyMap.get("GGCS_AREA")));
							
							String ZYSJQSSJTemp = parseXMLService.getMapValue(dlzyKeyMap.get("ZYSJQSSJ"));
							Date ZYSJQSSJ = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(ZYSJQSSJTemp)) {
								ZYSJQSSJ = DateUtils.stringToDate(ZYSJQSSJTemp, DateUtils.YYYY_MM_DD);
								form.setZysjqssj(ZYSJQSSJ);
							}
							
							String ZYSJJSSJTemp = parseXMLService.getMapValue(dlzyKeyMap.get("ZYSJJSSJ"));
							Date ZYSJJSSJ = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(ZYSJJSSJTemp)) {
								ZYSJJSSJ = DateUtils.stringToDate(ZYSJJSSJTemp, DateUtils.YYYY_MM_DD);
								form.setZysjjssj(ZYSJJSSJ);
							}
							form.setJgxssm(parseXMLService.getMapValue(dlzyKeyMap.get("JGXSSM")));
							form.setJgbmyj(parseXMLService.getMapValue(dlzyKeyMap.get("JGBMYJ")));
							
							String YJRQTemp = parseXMLService.getMapValue(dlzyKeyMap.get("YJRQ"));
							Date YJRQ = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(YJRQTemp)) {
								YJRQ = DateUtils.stringToDate(YJRQTemp, DateUtils.YYYY_MM_DD);
								form.setYjrq(YJRQ);
							}
							
							String TBRQTemp = parseXMLService.getMapValue(dlzyKeyMap.get("TBRQ"));
							Date TBRQ = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(TBRQTemp)) {
								TBRQ = DateUtils.stringToDate(TBRQTemp, DateUtils.YYYY_MM_DD);
								form.setTbrq(TBRQ);
								form.setSarq(TBRQ);
							}else{
								form.setSarq(new Date());
							}

							form.setZydw(parseXMLService.getMapValue(dlzyKeyMap.get("ZYDW")));
							form.setZydwAddress(parseXMLService.getMapValue(dlzyKeyMap.get("ZYDW_ADDRESS")));
							form.setZydwTel(parseXMLService.getMapValue(dlzyKeyMap.get("ZYDW_TEL")));
							form.setAqfzr(parseXMLService.getMapValue(dlzyKeyMap.get("AQFZR")));
							form.setZydwLxr(parseXMLService.getMapValue(dlzyKeyMap.get("ZYDW_LXR")));
							form.setApprovalNo(parseXMLService.getMapValue(dlzyKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(dlzyKeyMap.get("businessCode")));							
							
							String wstjsjStr = parseXMLService.getMapValue(dlzyKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setCbyw("城市道路人行道及相关公共场地临时占用审批");
							form.setCbbm(parseXMLService.getMapValue(dlzyKeyMap.get("CBBM")));
							form.setTbrq(new Date());//填表日期
							form.setBranch("csdllszysp");
							form.setSendNotice("Y");
							form.setYsl(1l);							
						}
						// 调用DAO，保存到数据库
						try {
							form.setSqdw(parseXMLService.getMapValue(dlzyKeyMap.get("SQDW")));
							ywDlzyService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									dlzyKeyMapHead, dlzyfeedbackPath, dlzyexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, dlzyfujianpath, dlzyfeedbackPath, dlzyexceptionPath, dlzyKeyMapHead,
								files, dlzyfujianMap, i, Integer.parseInt(form.getQybh()), form.getId(), "csdllszysp");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								dlzyKeyMapHead, dlzyfeedbackPath, dlzyexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, dlzybackPath + fileName,
							fileName);
				}
			}
		}
	}

}
